import qqmapsdk from '../../../libs/index'
Page({
  data: {
    list: [],
    address: ''
  },
  async onLoad() {
    // 获取地理位置
    const {
      latitude,
      longitude
    } = await wx.getLocation()
    this.showLocationInfo(latitude, longitude)
  },
  showLocationInfo(latitude, longitude) {
    // 附近社区搜索
    qqmapsdk.search({
      keyword: '住宅小区',
      page_size: 5,
      location: [latitude, longitude].join(','), // 待会设置为自己的经纬度
      success: (res) => {
        this.setData({
          list: res.data.map(item => ({
            id: item.id,
            title: item.title
          }))
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    })
    // 逆地址解析
    qqmapsdk.reverseGeocoder({
      location: [latitude, longitude].join(','),
      success: (res) => {
        // console.log(res.result.address);
        this.setData({
          address: res.result.address
        })
      }
    })
  },
  // 重新定位
  async chooseLocation() {
    const res = await wx.chooseLocation()
    this.showLocationInfo(res.latitude, res.longitude)
  }
})